﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Store.Web.Services;
using System.Web.Configuration;
using Store.Web.Models;

namespace Store.Web.Controllers
{
    public class OrderController : Controller
    {
        private readonly OrderService _orderService;
        public OrderController()
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["StoreDb"].ConnectionString;
            _orderService = new OrderService(connectionString);
        }

        public ActionResult Index()
        {
            var commands = _orderService.GetOrders();
            return View(commands);
        }

        public ActionResult Edit(int id)
        {
            var command = _orderService.GetOrder(id);
            var states = new List<SelectListItem>();
            states.Add(new SelectListItem { Value = "1", Text = "En cours de validation" });
            states.Add(new SelectListItem { Value = "2", Text = "En cours de préparation" });
            states.Add(new SelectListItem { Value = "3", Text = "Expédiée" });
            states.Add(new SelectListItem { Value = "4", Text = "Annulée" });

            ViewBag.States = states;

            return View(command);
        }

        [HttpPost]
        public ActionResult Edit(Order command)
        {
            if (ModelState.IsValid)
            {
                _orderService.UpdateOrderState(command.Id, command.State);
                return View("Index", _orderService.GetOrders());
            }

            return View(command);
        }
    }
}
